package Z.dailyExercise._0109;

public class _38_外观数列 {

    public static void main(String[] args) {
        _38_外观数列 test = new _38_外观数列();
        System.out.println(test.countAndSay(4));
    }

    /**
     * 递归函数实现
     * @param n
     * @return
     */
    public String countAndSay(int n) {
        return helper(n,new StringBuffer("1")).toString();

    }

    public StringBuffer helper(int n,StringBuffer newStr){

        if (n==1){
            return newStr;
        }

        StringBuffer sb = new StringBuffer();
        for (int i=0;i<newStr.length();i++){
            char temp = newStr.charAt(i);
            int index = 1;//表示当前字符的个数

            while ( i+1<newStr.length() && newStr.charAt(i+1)== temp  ){
                i++;
                index++;
            }
            sb.append(index);
            sb.append(temp);
        }
        n--;


        return helper(n,sb);

    }
}
